<%@page import="cn.egame.interfaces.gc.GameInfo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="cn.egame.common.model.PageData"%>
<%@page import="java.util.*"%>
<%@page import="cn.egame.interfaces.pu.AppParameter"%>
<%@page import="cn.egame.interfaces.gc.GameInfo"%>  
<%@page import="cn.egame.interfaces.gc.GameTagLinkInfo"%> 
<%@page import="com.egame.manage.biz.server.pu.ParameterServiceImpl"%>
<%@page import="com.egame.manage.biz.utils.ConstVar"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2.min.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/main.css"  />
<title>推荐列表</title>
</head>
<body>
<script type="text/javascript">
	$(document).ready(function() {
		if($("#position").val()==0) {
			$("input.hotButton").hide();
		}
	});

	function getRecommendList(){		
		var myform = document.getElementById("myform");
		myform.action = myform.action + getOptionValue("position");
		myform.submit();		
	}
	
	function checkValue(position,recommend){
		positionValue = getOptionValue(position);		
		recommendValue = getOptionValueList(position);
		
		if(positionValue == null || positionValue == ""){
			alert("请选择推荐位置");
			return;
		}
		if(positionValue == null || positionValue == ""){
			alert("请选择推荐游戏");
			return;
		}
	}
	
	function setHot() {	
		checkValue("position","recommendList");
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/tag/hotflag/set.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("recommendList").toString(),
				'tagname' : getOptionText("position"),
				'hotflag' : getFlagList("recommendList","HOT").toString()
			},
			success : function(msg) {	
				setFlagList("recommendList","HOT--");				
			}
		});
	}

	function setNew() {	
		checkValue("position","recommendList");		
		alert(getOptionText("position"));
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/tag/newflag/set.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("recommendList").toString(),
				'tagname' : getOptionText("position"),
				'newflag' : getFlagList("recommendList","NEW").toString()
			},
			success : function(msg) {	
				setFlagList("recommendList","NEW--");	
			}
		});
	}
	
	function getOptionValueList(selectId){
		var options = document.getElementById(selectId);
		var result = new Array();
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				result.push(options[i].value);
			}
		}
		return result;
	}

	function getFlagList(selectId,flag){
		var options = document.getElementById(selectId);
		var result = new Array();
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				result.push(options[i].text.indexOf(flag)>=0?0:1);
			}
		}
		return result;
	}

	function setFlagList(selectId,flag){
		var options = document.getElementById(selectId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				if(options[i].text.indexOf(flag)>=0){
					options[i].text = options[i].text.split(flag)[1];
				}else{
					options[i].text = flag + options[i].text;
				}				
			}
		}
	}
	
	function getOptionValue(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		return options[optionIndex].value;
	}
	
	function getOptionText(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		return options[optionIndex].text;
	}
	
	function setOptionText(selectName,text){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		options[optionIndex].text = text;
	}
	
	function removeOption(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		options.remove(optionIndex);
	}

	function addOption(selectName,value,text){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		options.add(new Option(text,value));		
		options[options.length-1].selected = "selected";
	}
	
	function moveOption(fromId,toId){
		var options = document.getElementById(fromId);
		var toOptions = document.getElementById(toId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				toOptions.add(new Option(options[i].text,options[i].value));
				options.remove(i);
				--i;
			}
		}
	}
	
	function moveToOption(fromId,toId){
		var options = document.getElementById(fromId);
		var toOptions = document.getElementById(toId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				toOptions.options.add(new Option(options[i].text,options[i].value),0);
				options.remove(i);
				--i;
			}
		}
	}

	function upper() {var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要上移的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/upper.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index()>0){
				optionIndex.insertBefore(optionIndex.prev());
			}
		}
	});		
}

function lower() {
	var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要下移的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/lower.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index() < (recommendSelect.find("option").length-1)){
				optionIndex.insertAfter(optionIndex.next());
			}
		}
	});		
	
}

function toTop() {
	var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要置顶的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/top.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index()>0){
				optionIndex.insertBefore(recommendSelect.find("option").first());
			}
		}
	});		
}

function toDown(){
	var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要置底的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/down.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index() < (recommendSelect.find("option").length-1)){
				optionIndex.insertAfter(recommendSelect.find("option").last());
			}
		}
	});	
	
}
	
	function addRecommendGame(){
		var values = getOptionValueList("gameList");
		if(document.getElementById("position").value=="0"){
			alert("请先选择推荐位置！");
			return;
		}
		if(values.toString()==""){
			alert("请选择要推荐的游戏！");
			return;
		}					
		var gameValue = getOptionValue("gameList");	
		var recommendOptions = document.getElementById("recommendList").options;
		for(var i=0;i<recommendOptions.length;i++){
	        if(recommendOptions[i].value == gameValue){
	            alert("已添加该游戏");
	            return true;
	        }
	    }     
		
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/tag/link/add.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("gameList").toString(),
				'tagid' : $('#position').val()
				
			},
			success : function(msg) {
				moveToOption("gameList","recommendList");
			}
		});		
	}
	
	function deleteRecommendGame(){
		positionValue = getOptionValue("position");
		positionText = getOptionText("position");		
		recommendValue = getOptionValue("recommendList");
		recommendText = getOptionText("recommendList");
		
		if(positionValue == null || positionValue == ""){
			alert("请选择推荐位置");
			return;
		}
		if(recommendValue == null || recommendValue == ""){
			alert("请选择推荐游戏");
			return;
		}
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/tag/link/delete.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("recommendList").toString(),
				'tagid' : positionValue				
			},
			success : function(msg) {	
				moveOption("recommendList","gameList");
			}
		});	
	}
	
</script>
<form id="myform" action="<%=request.getContextPath() + "/tv/parameter/list/recommend.do?position=" %>" method="post">
<% 
	int postion = 0;
	if(request.getAttribute("position")!=null){
		postion = (Integer)request.getAttribute("position");
	}
	String gameName = "";
	if(request.getAttribute("gameName")!=null){
		gameName = (String)request.getAttribute("gameName");
	}
	String recommendName = "";
	if(request.getAttribute("recommendName")!=null){
		recommendName = (String)request.getAttribute("recommendName");
	}
%>
	<div class="matter_box">
		<h3 class="nopoint"><strong>推荐列表</strong></h3>
		<div class="task_search">
			<span>推荐位置：</span>
			<input type="hidden" name="postionName" id="postionName">
			<select id="position" name="position" onchange="getRecommendList()">
				<option value="0" selected>请选择</option>
				<%
					if(request.getAttribute("positionList")!=null){
						List<AppParameter> list = (List<AppParameter>)request.getAttribute("positionList");
						for(AppParameter bean:list){
							%>
							<option value="<%=bean.getId() %>" 
								<%	if(bean.getId() == postion){
										%> selected="selected" <%
									}
									%>
								
							><%=bean.getName() %></option>
							<%
						}
					}
				%>
			</select>
		</div>
		<div class="mt10 pt10 clearfix">
<table>
	<tr>
		<td>
			<table>				
				<tr>
					<td width=35% align=center>游戏列表</td>
					<td></td>
					<td width=30% align=center>推荐列表</td>
					<td></td>					
				</tr>	
				<tr>
					<td colspan=2>
						<span>游戏名称：</span><input type=text name="gname" value="<%=gameName %>">
						<input type=button value="查询" class="btn3" onclick="javascript:getRecommendList()">
					</td>
					<td colspan=2>
						<span>游戏名称：</span><input type=text name="recommendname"  value="<%=recommendName %>">
						<input type=button value="查询" class="btn3" onclick="javascript:getRecommendList()">
					</td>					
				</tr>			
				<tr>
					<td>
						<select name="gameList" id="gameList" size="20" style="width:240px;" multiple="multiple">
							<%
								if(request.getAttribute("gameList")!=null){
									List<GameInfo> list = (List<GameInfo>)request.getAttribute("gameList");
									for(GameInfo bean:list){
										%>
											<option id="<%=bean.getGId() %>" value=<%=bean.getGId() %>>												
												[<%=bean.getGId() %>]--
												<%=bean.getGameName() %>--
												(<%=bean.getOsCode() %>)
											</option>
										<%
									}
								}
								
							%>
						</select>
					</td>
					<td>
						<input type=button value="添加 >" class="btn3"  onclick="addRecommendGame()"> <br/>
						<input type="button" class="btn3" onclick="deleteRecommendGame()" value="< 删除"><br/>
					</td>
					
					<td>
						<select name="recommendList" id="recommendList" size="20" style="width:240px;"  multiple="multiple">
							<%
								Map<Integer, GameTagLinkInfo> map = new HashMap<Integer, GameTagLinkInfo>();
								if(request.getAttribute("map")!=null){
									map = (Map<Integer, GameTagLinkInfo>)request.getAttribute("map");
								}
							
								if(request.getAttribute("recommendGameList")!=null){
									List<GameInfo> list = (List<GameInfo>)request.getAttribute("recommendGameList");
									for(GameInfo bean:list){
										%>
											<option id="<%=bean.getGId() %>" value=<%=bean.getGId() %>>
												<% if(map.containsKey(bean.getGId())){
														if(postion==3){
												%>NEW--<% }else{
												%>HOT--<%}} %>
												[<%=bean.getGId() %>]--
												<%=bean.getGameName() %>--
												(<%=ParameterServiceImpl.getInstance().getParameterNameById(0,0L,ConstVar.APP_OS,""+bean.getOsCode()) %>)
											</option>
										<%
									}
								}
								
							%>
						</select>
					</td>
					<td>
						<% if(postion == 3){ %>
							<input type="button" class="btn3 hotButton" onclick="setNew()" value="NEW"><p>
						<% }else{ %>
							<input type="button" class="btn3 hotButton" onclick="setHot()" value="HOT"><p>
						<% } %>
						<input type="button" class="btn3" onclick="upper()" value="上移"><p>
						<input type="button" class="btn3" onclick="lower()" value="下移"><p>
						<input type="button" class="btn3" onclick="toTop()" value="置顶"><p>
						<input type="button" class="btn3" onclick="toDown()" value="置底"><p>
						
						
					</td>					
				</tr>
			</table>
		</td>
	</tr>
</table>
</div>
</div>
</form>
</body>
</html>